package com.broada.apm.indicator.api.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.broada.apm.dao.RelationDao;
import com.broada.apm.dao.ServiceDao;
import com.broada.apm.indicator.api.RelationService;
import com.broada.apm.model.ProtocolRelation;
import com.broada.apm.model.Relation;
import com.broada.apm.model.Relations;
import com.broada.apm.model.Service;
import com.broada.apm.model.ServiceTopology;
import com.broada.apm.model.TopologyRelation;

public class RelationServiceImpl implements RelationService {

	@Autowired
	private RelationDao relationDao;
	@Autowired
	private ServiceDao serviceDao;
	
	@Override
	public void addRelation(Relation relation) {
		relationDao.addRelation(relation);
	}

	@Override
	public void deleteRelation(Relation relation) {
		relationDao.deleteRelation(relation);
	}
	
	@Override
	public void deleteAllDisRelation() {
		relationDao.deleteAllDisRelation();
	}

	@Override
	public Relation getRelationById(String id) {
		return relationDao.getRelationById(id);
	}

	@Override
	public List<Relation> getRelationByAppId(String appId) {
		return relationDao.getRelationByAppId(appId);
	}

	@Override
	public void deleteRelationByAppId(String appId) {
		relationDao.deleteRelationByAppId(appId);
	}

	@Override
	public List<Relation> getRelationByFromOrTo(String[] serviceIds) {
		return relationDao.getRelationByFromOrTo(serviceIds);
	}

	@Override
	public void deleteRelationByFromOrTo(String[] serviceIds) {
		relationDao.deleteRelationByFromOrTo(serviceIds);
	}

	@Override
	public List<Relation> getRelationByFromAndTo(String[] fromSerIds,
			String[] toSerIds) {
		return relationDao.getRelationByFromAndTo(fromSerIds, toSerIds);
	}

	@Override
	public List<ProtocolRelation> getDisRelationBySrcIp(Long srcIp) {
		return relationDao.getDisRelationBySrcIp(srcIp);
	}

	@Override
	public List<ProtocolRelation> getDisRelationByServiceId(String serviceId) {
		return relationDao.getDisRelationByServiceId(serviceId);
	}

	@Override
	public ProtocolRelation getDisRelationById(String relationId) {
		return relationDao.getDisRelationById(relationId);
	}

	@Override
	public void addDisRelation(ProtocolRelation relation) {
		relationDao.addDisRelation(relation);
	}

	@Override
	public void deleteDisRelation(ProtocolRelation relation) {
		relationDao.deleteDisRlation(relation);
		
	}

	@Override
	public List<ProtocolRelation> getDisRelationByService(Service service) {
		return relationDao.getDisRelationByService(service);
	}

	@Override
	public List<Relation> getServiceRelation(List<Service> serviceList) {
		//获取所有的关系
		List<ProtocolRelation> allRelation = relationDao.getAllDisRelation();
		List<Relation> relations = new ArrayList<Relation>();
		if(allRelation != null && allRelation.size() > 0){
			//遍历关系
			for(ProtocolRelation relation:allRelation){
				String toServiceId = relation.getServiceId();
				long srcIp = relation.getSrcIp();
				// 取每条关系的来源关系
				List<Service> services = new ArrayList<Service>();
				for(Service ser : serviceList){
					if(ser.getIpNum() == srcIp){
						services.add(ser);
					}
				}
				if(services != null && services.size() > 0){
					for(Service fromService:services){
						String fromServiceId = fromService.getId();
						Relation rel = new Relation();
						rel.setTo(toServiceId);
						rel.setFrom(fromServiceId);
						relations.add(rel);
					}
				}
			}
		}
		return relations;
	}

	@Override
	public List<ServiceTopology> getServiceRelationTopology() {
		List<ServiceTopology> list = new ArrayList<ServiceTopology>();
		List<Relations> allRelation = relationDao.getAllDisRelationTopology();
		if(allRelation != null && allRelation.size() > 0){
			for(Relations relation : allRelation){
				ServiceTopology topology = new ServiceTopology();
				if(StringUtils.isNotBlank(relation.getFrom())){
					Service srcService = serviceDao.getDisServiceById(relation.getFrom());
					if(srcService != null && needRet(srcService.getPort())){
						topology.setFrom(srcService);
					}
				}
				if(StringUtils.isNotBlank(relation.getTo())){
					Service desService = serviceDao.getDisServiceById(relation.getTo());
					if(desService != null && needRet(desService.getPort())){
						topology.setTo(desService);
					}
				}
				list.add(topology);
			}
		}
		return list;
	}
	
	
	@Override
	public List<TopologyRelation> getRelationTopology() {
		return relationDao.getRelationTopology();
	}
	
	/**
	 * 根据port判断是否需要返回
	 * @param port
	 * @return
	 */
	private boolean needRet(int port){
		if(port == 22 || port == 23 || port == 137
		   ||port == 138 || port == 139 || port == 445){
			return false;
		}
		return true;
	}

	@Override
	public void deleteValidRelation() {
		relationDao.deleteValidRelation();
	}
}
